﻿using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace NfsBLL
{/// <summary>
    /// agency
    /// </summary>
    public partial class AgencyBLL
    {
        private readonly NfsDAL.AgencyDAL dal = new NfsDAL.AgencyDAL();
        public AgencyBLL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }
        public bool UpdateAucode(string au_code,int id) { 
            return dal.UpdateAucode(au_code,id);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(NfsModel.agency model, out int id)
        {
            return dal.Add(model, out id);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(NfsModel.agency model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(Maticsoft.Common.PageValidate.SafeLongFilter(idlist, 0));
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public NfsModel.agency GetModel(int id)
        {

            return dal.GetModel(id);
        }
        public agency GetModel2(int id, int auid)
        {

            return dal.GetModel2(id, auid);
        }
        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public NfsModel.agency GetModelByCache(int id)
        {

            string CacheKey = "agencyModel-" + id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (NfsModel.agency)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<NfsModel.agency> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<NfsModel.agency> DataTableToList(DataTable dt)
        {
            List<NfsModel.agency> modelList = new List<NfsModel.agency>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                NfsModel.agency model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  自定义方法
        #region PC模糊查询
        public List<agency> GetAgencyBycondtion(int pageindex, int pagesize, int keys, string keyName, int category_id, string zhuangtai)
        {
            DataTable dt = dal.GetAgencyBycondtion(pageindex, pagesize, keys, keyName, category_id, zhuangtai);
            return ConverToExcs.ConvertToEx<agency>(dt);
        }
        public object GetAgencyBycondtionCount(int keys, string keyName, int category_id, string zhuangtai)
        {
            return dal.GetAgencyBycondtionCount(keys, keyName, category_id, zhuangtai);
        }
        #endregion
        #region 修改和添加用户判断用户信息是否有重复信息
        /// <summary>
        /// 插叙
        /// </summary>
        /// <param name="name"></param>
        /// <param name="mobile"></param>
        /// <param name="qq"></param>
        /// <param name="wx_openid"></param>
        /// <param name="ww_code"></param>
        /// <param name="id_card"></param>
        /// <param name="shop_url"></param>
        /// <returns></returns>
        public bool GetSingle(int id, string name, string mobile, string qq, string wx_openid, string ww_code, string id_card, string shop_url)
        {
            return dal.GetSingle(id, name, mobile, qq, wx_openid, ww_code, id_card, shop_url);
        }
        #endregion
        #region 修改授权图片
        public bool UptSq_img(int id,string img_url)
        {
            return dal.UptSq_img(id,img_url);

        }
        #endregion
        #region 修改授权图片
        public bool UptSq_imgAndCode(int id, string au_code, string img_url)
        {
            return dal.UptSq_imgAndCode(id, au_code, img_url);

        }
        #endregion

        #region 修改和新增用户信息
        public bool InsertAgency(agency ag, account act,out int agency_id)
        {

            bool flag = true;
            int agId = 0;
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    int act_id = 0;
                    int ag_id = 0;
                    AccountBLL act_bll = new AccountBLL();
                    flag = act_bll.Add(act, out act_id);
                    ag.account_id = act_id;
                    if (act_id>0)
                    {
                        flag = this.Add(ag, out ag_id);

                    }
                    else {
                        flag = false;
                    }
                    if (flag)
                    {
                        trans.Complete();
                        agId = ag_id;
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }
            agency_id = agId;
            return flag;



        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="ag"></param>
        /// <param name="act"></param>
        /// <returns></returns>
        public bool UpdateAgency(agency ag, account act,out int agency_id)
        {

            bool flag = true;
            int agId = 0;
            
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    AccountBLL act_bll = new AccountBLL();
                    agency model = this.GetModel(ag.id);
                    if (model != null)
                    {
                        act.id =(int)model.account_id;
                        flag = act_bll.Update(act);
                        if (flag)
                        {
                            ag.account_id = act.id;
                            flag = this.Update(ag);
                            if (model.parents_id != 0 && model.parents_id!=null&& flag)
                            {
                                ShareBLL sh_bll = new ShareBLL();
                                int state = ag.shState == 1 ? 2 : 3;
                               flag=sh_bll.Update_state(state,ag.id);
                            
                            }

                        }
                        else {
                            flag = false;
                        }
                    }
                    else
                    {
                        flag = false;
                    }

                    if (flag)
                    {
                        trans.Complete();
                        agId = ag.id;
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }
            agency_id = agId;

            return flag;


        }
        #endregion
        #region 删除用户信息
        public bool deleteAgency(int id)
        {

            bool flag = true;

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    AccountBLL act_bll = new AccountBLL();
                    agency model = this.GetModel(id);
                    if (model != null)
                    {
                        flag = act_bll.com_delelte((int)model.account_id, "t_account", "valid", 0);
                        if (flag)
                        {

                            flag = act_bll.com_delelte(id, "t_agency", "valid", 0);
                        }
                        if (flag)
                        {
                            trans.Complete();
                        }
                    }
                    else {
                        flag = false;
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                }
            }

            return flag;
        }
        #endregion
        public NfsModel.agency GetModelCode(agency ag, string coding)
        {
            return dal.GetModelCode(ag,coding);
        }
           #region  申请进行授权
        /// <summary>
        /// 进行授权
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <param name="level_id"></param>
        /// <param name="start_time"></param>
        /// <param name="end_time"></param>
        /// <param name="levelName"></param>
        /// <returns></returns>
        public bool UptPassOrNo_pc(int id, int state, int level_id, DateTime start_time, DateTime end_time, string levelName)
        {
            return dal.UptPassOrNo_pc(id, state, level_id, start_time, end_time, levelName);
        }
           #endregion

        #region 导出数据
        public List<AgencyExportModel> GetAgencyBycondtionList(int keys, string keyName, int category_id, string zhuangtai)
        
        {
            DataTable dt = dal.GetAgencyBycondtionList(keys, keyName, category_id, zhuangtai);
            return ConverToExcs.ConvertToEx<AgencyExportModel>(dt);
        }

        #endregion
         #region (手机)根据登录用户获取客户的信息
        public NfsModel.agency GetModel_account(int act_id)
        {
            return dal.GetModel_account(act_id);
        }
         #endregion
        #region 修改是否审核  /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update_shState(NfsModel.agency model)
        {
            return dal.Update_shState(model);
        }
        #endregion

      #region 根据授权编号或者身份证号码获取用户的授权证书
        /// <summary>
        /// 根据授权编号或者身份证号码获取用户的授权证书
        /// </summary>
        /// <param name="codes"></param>
        /// <returns></returns>
        public NfsModel.agency GetModel_ByCodes(string codes)
        {
            return dal.GetModel_ByCodes(codes);
        }
           #endregion
        #endregion  ExtensionMethod
    }
}

